const { getMyFeedback } = require("../../utils/request");
const config = require("../../utils/config");

Page({
  data: {
    feedbackList: [],
  },

  onLoad() {
    this.loadMyFeedback();
  },

  onShow() {
    // 每次页面显示时重新加载反馈数据
    this.loadMyFeedback();
  },

  async loadMyFeedback() {
    try {
      const res = await getMyFeedback();
      if (res.code === "00000" && res.data) {
        this.setData({
          feedbackList: res.data.map(item => ({
            ...item,
            createdAt: this.formatDate(item.createdAt),
            imageUrls: JSON.parse(item.imageUrls || "[]").map(url => config.ImageURL + url),
            statusText: this.getStatusText(item.status),
            statusClass: this.getStatusClass(item.status)
          }))
        });
      } else {
        wx.showToast({ title: "Failed to load feedback.", icon: "none" });
      }
    } catch (err) {
      wx.showToast({ title: "Network error. Please try again.", icon: "none" });
    }
  },

  formatDate(dateString) {
    const date = new Date(dateString);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
  },

  getStatusText(status) {
    return ["未处理", "处理中", "已处理"][status] || "未知状态";
  },

  getStatusClass(status) {
    return ["status-pending", "status-processing", "status-done"][status] || "";
  },

  previewImage(e) {
    const imageUrl = e.currentTarget.dataset.url;
    wx.previewImage({ urls: [imageUrl] });
  },

  // 跳转到新增反馈页面
  navigateToFeedback() {
    wx.navigateTo({
      url: "/pages/feedback/feedback",  // 跳转到反馈页面
    });
  },
});
